package com.library.controller;

import com.library.bean.Admin;
import com.library.bean.ReaderInfo;
import com.library.service.AdminBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;

/*权限管理  相关Controller层           1、管理员信息增删改查  2、变更权限 */
@Controller
public class BorrowAdminController {
    @Autowired
    private AdminBorrowService adminBorrowService;

    /*查看全部管理员信息*/
    @RequestMapping("allBorrowAdmin.html")
    public ModelAndView allBorrowAdmin() {
        ArrayList<Admin> borrowAdmins = adminBorrowService.findAllAdmainBorrow();
        ModelAndView modelAndView = new ModelAndView("admin_borrow");
        modelAndView.addObject("borrowAdmins", borrowAdmins);
        return modelAndView;
    }

    /*编辑管理员信息 (第一步)*/
    @RequestMapping("admin_borrow_edit.html")
    public ModelAndView readerInfoEdit(HttpServletRequest request) {
        long adminId = Long.parseLong(request.getParameter("adminId"));
        Admin borrowAdmin = adminBorrowService.findAdmainBorrowById(adminId);
        ModelAndView modelAndView = new ModelAndView("admin_borrow_edit");
        modelAndView.addObject("borrowAdmin", borrowAdmin);
        return modelAndView;
    }

    /*编辑管理员信息 (第二步)*/
    @RequestMapping("admin_borrow_edit_do.html")
    public String readerInfoEditDo(HttpServletRequest request, String password, String username, RedirectAttributes redirectAttributes) {
        //主键需要这样获取
        long adminId = Long.parseLong(request.getParameter("adminId"));
        Admin borrowAdmin = new Admin();
        borrowAdmin.setAdminId(adminId);
        borrowAdmin.setPassword(password);
        borrowAdmin.setUsername(username);
        if (adminBorrowService.updateAdminBorrowMessage(borrowAdmin)) {
            redirectAttributes.addFlashAttribute("succ", "借阅管理员信息修改成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "借阅管理员信息修改失败！");
        }
        return "redirect:/allBorrowAdmin.html";
    }

    /*-------------------添加这里用到了ajax(账号为主键，不能重复)------------------------------*/
    /*添加管理员信息(第一步)*/
    @RequestMapping("admin_borrow_add.html")
    public ModelAndView readerInfoAdd() {
        return new ModelAndView("admin_borrow_add");
    }

    /*添加管理员信息(第二步)       ★★★★★ajax判断账号是否存在          这块和LoginController中读者注册类似*/
    @RequestMapping(value = "/api/AdminBorrowCheck", method = RequestMethod.POST)
    public @ResponseBody
    Object AdminBorrowCheck(HttpServletRequest request) {
        //解决因账号存在，自动清空后传递空字符串Long.parseLong()异常
        if("".equals(request.getParameter("id"))){
            return  null;
        }
        long id = Long.parseLong(request.getParameter("id"));
        boolean exit = false;   //账号是否存在
        //和已经注册的读者账号重复
        ArrayList<Admin> adminBorrow= adminBorrowService.findAllAdmainBorrow();
        for (int i = 0 ; i < adminBorrow.size(); i++){
            if(id == adminBorrow.get(i).getAdminId()){
                exit = true;
            }
        }
        HashMap<String, String> res = new HashMap<>();
        if (id>99999 && id<=999999 &&!exit)
            res.put("states", "1");
        if (id<99999 || id>999999 )
            res.put("states", "2");
        if (id>99999 && id<=999999 &&exit)
            res.put("states", "3");
        return res;
    }

    /*添加管理员信息(第三步)*/
    @RequestMapping("admin_borrow_add_do.html")
    public String readerInfoAddDo(Admin borrowAdmin, RedirectAttributes redirectAttributes) {
        if (adminBorrowService.addAdmainBorrow(borrowAdmin)) {
            redirectAttributes.addFlashAttribute("succ", "添加借阅管理员成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "添加借阅管理员失败！");
        }
        return "redirect:/allBorrowAdmin.html";
    }

    /*删除管理员*/
    @RequestMapping("admin_borrow_delete.html")
    public String readerDelete(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        long adminId = Long.parseLong(request.getParameter("adminId"));
        if (adminBorrowService.deleteAdminBorrow(adminId)) {
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
        }
        return "redirect:/allBorrowAdmin.html";
    }

    /*变更管理员权限*/
    @RequestMapping("changPower.html")
    public String changPower(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        long adminId = Long.parseLong(request.getParameter("adminId"));
        int power = Integer.parseInt(request.getParameter("power"));
        Boolean powerIsChange = false;
        if(power == 1 )
            powerIsChange = adminBorrowService.updatePowerToAdminBorrow(adminId);
        else
            powerIsChange = adminBorrowService.updatePowerToAdmin(adminId);

        if (powerIsChange) {
            redirectAttributes.addFlashAttribute("succ", "修改权限成功！");
        } else {
            redirectAttributes.addFlashAttribute("error", "修改权限失败！");
        }
        return "redirect:/allBorrowAdmin.html";
    }

}
